//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using System.Collections;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.Controls;
using CommunityServer.Discussions.Components;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Forums
{

	/// <summary>
	///		Summary description for GroupEditControl.
	/// </summary>
	public class GroupEditControl : BaseForumControl
	{

		#region Child Controls

		protected TextBox ForumGroupName;
		protected TextBox ForumGroupDesc;
		protected RequiredFieldValidator ForumGroupNameValidator;
		protected StatusMessage Status;

		#endregion

		#region Public Properties

		private int groupID = -1;
		public int GroupID
		{
			get{return groupID;}
			set{groupID = value;}
		}

		public string CurrentGroupName
		{
			get{return ForumGroupName.Text;}
		}

		private bool allowDelete = true;
		public bool AllowDelete
		{
			get {return allowDelete;}
			set {allowDelete = value;}
		}

		#endregion

		private bool IsNew
		{
			get{return GroupID == -1;}
		}


		override protected void OnInit(EventArgs e)
		{
			this.Load += new System.EventHandler(this.Page_Load);
			base.OnInit(e);
		}


		private void Page_Load(object sender, System.EventArgs e)
		{
			if ( !Page.IsPostBack ) 
			{
				this.Bind();
			}
		}


		protected void Bind()
		{
			Status.Visible = false;

			if(IsNew)
			{
				this.AllowDelete = false;
			}
			else
			{
				CommunityServer.Components.Group group = CurrentSectionGroup();
				if( group != null )
				{
					LoadGroup(group);
					if (group.HasSections)
						this.AllowDelete = false;
				}
				else
					this.AllowDelete = false;
			}
		}


		protected void LoadGroup(Group group) 
		{
			ForumGroupName.Text = group.Name;
			ForumGroupDesc.Text = group.Description;
		}


		protected Group CurrentSectionGroup()
		{
			CSContext cntx = CSContext.Current;
			if(cntx.User.IsForumAdministrator)
				return ForumGroups.GetForumGroup(cntx.GroupID, false, true, cntx.UserID);
			else
				throw new CSException(CSExceptionType.AdministrationAccessDenied);
		}


		protected ArrayList GetGroups()
		{
			return ForumGroups.GetForumGroups(false, true, true);
		}


		public bool Save()
		{
			Status.Success = false;
			Status.Visible = true;
			Status.ResourceName = "CP_Forums_GroupEdit_StatusFailed";

			if(Page.IsValid)
			{
				if(IsNew)
				{
					Group group = new Group();
					group.ApplicationType = ApplicationType.Forum;

					if (SaveGroup(group))
					{
						Status.Success = true;
						Status.ResourceName = "CP_Forums_GroupEdit_StatusAddSuccess";
					}

				}
				else
				{
					Group group = CurrentSectionGroup();
					if (SaveGroup(group))
					{
						Status.Success = true;
						Status.ResourceName = "CP_Forums_GroupEdit_StatusEditSuccess";
					}
				}

			}

			return Status.Success;
		}


		protected virtual bool SaveGroup(Group group)
		{
			group.Name = ForumGroupName.Text.Trim();
			group.Description = ForumGroupDesc.Text;

			foreach(Group g in GetGroups())
			{
				if((g.Name.ToLower()  == group.Name.ToLower() ) && (g.GroupID != group.GroupID))
				{
					ForumGroupNameValidator.IsValid = false;
					Status.ResourceName = "CP_Forums_GroupEdit_DuplicateNameException";
					return false;
				}
			}

			if(Page.IsValid)
			{
				if(IsNew)
					Groups.AddGroup( group ); 
				else
					Groups.UpdateGroup( group );

				GetGroups();
				return true;
			}
			else
			{
				return false;
			}
		}


		public void Delete() 
		{
			CommunityServer.Components.Group group = ForumGroups.GetForumGroup(this.GroupID, false, true, CSContext.Current.UserID);

			// Don't delete a group if it has sections
			if(!ForumGroups.HasChildSections(GroupID))
				Groups.DeleteGroup(group);
		}

	}
}
